#include "player.h"
#include <QGraphicsItem>
#include <QTimer>
#include <QPropertyAnimation>

const double speed=30.0f;
const int WIDTH=110;
const int HEIGHT=200;

Player::Player(QObject *parent):QObject(parent){
    Player("p1");

}

Player::Player(QString name)
{

    playerName=name;
    offx=0;
    offy=0;
    areaActive=false;
    flip=false;
//    skillPoint=0;
    status=standRight;
    graphics=new QGraphicsPixmapItem();

    QString movieName="://"+playerName+"_stand.gif";
    movie = new QMovie(movieName);
    movie->start();
    actionFlag=true;
    attackFlag=true;
    cdTimer=new QTimer();

    connect(cdTimer,&QTimer::timeout,[&](){
        if(attackCD>0){
            attackCD--;
        }
        if(defenseCD>0){
            defenseCD--;
        }
        if(combinationAttackCD>0){
            combinationAttackCD--;
        }
    });
    cdTimer->start(100);
    skillGifTimer=new QTimer();
    connect(skillGifTimer,&QTimer::timeout,[&](){
        qDebug()<<"skill timeout"<<endl;
        if(status==combinationAttackLeft){
            status=standLeft;
        }else if(status==combinationAttackRight){
            status=standRight;
        }
        skillGifTimer->stop();
    });
    attackedGifTimer=new QTimer();
    connect(attackedGifTimer,&QTimer::timeout,[&](){
        qDebug()<<"attacked gif timeout"<<endl;
        if(status==attackedRight){
            qDebug()<<"sss"<<endl;
            status=standRight;
        }else if(status==attackedLeft){
            status=standLeft;
            qDebug()<<"ttt"<<endl;
        }
        attackedGifTimer->stop();
    });


    attackGifTimer=new QTimer();
    connect(attackGifTimer,&QTimer::timeout,[&](){
        if(status==attackRight){
            qDebug()<<"attack gif timeout"<<endl;
            status=standRight;
        }else if(status==attackLeft){
            status=standLeft;
        }
        attackGifTimer->stop();
    });
    jumpTimer=new QTimer();
    connect(jumpTimer,&QTimer::timeout,[&](){
        qDebug()<<"jumpingTimer"<<endl;
        status=standRight;
        jumpTimer->stop();
    });
}
void Player::run(){
    b2Vec2 pos = body->GetPosition();
//    qDebug()<<"p1:"<<pos.x<<","<<pos.y<<"v:"<<body->GetLinearVelocity().x<<endl;
//    qDebug()<<"attackCD:"<<attackCD<<endl;
//    qDebug()<<"area:"<<areaActive<<endl;
    graphics->setOffset(QPointF(0,0));
    if(status==moveRight){
        if(actionFlag){

            QString fileName="://"+playerName+"_move.gif";
            if(movie->fileName()!=fileName){
                movie->stop();
                movie->setFileName(fileName);
                movie->start();
            }


            flip=false;

            actionFlag=true;
        }
        body->SetLinearVelocity(b2Vec2(speed,0.0f));
    }else if(status==moveLeft){
        if(actionFlag){
            QString fileName="://"+playerName+"_move.gif";
            if(movie->fileName()!=fileName){
                movie->stop();
                movie->setFileName(fileName);
                movie->start();
            }
            flip=true;
            actionFlag=true;
        }
        body->SetLinearVelocity(b2Vec2(-speed,0.0f));

    }else if(status==jumpUpRight){
        qDebug()<<"jumpUpRight"<<endl;
        if(actionFlag){
            movie->stop();
            movie->setFileName("://"+playerName+"_jump.gif");
            movie->start();
            flip=false;
            actionFlag=false;
            jumpTimer->start(700);
        }
        body->SetLinearVelocity(b2Vec2(0.0f,speed));
    }
    else if(status==jumpUpLeft){
        qDebug()<<"jumpUpRight"<<endl;

        if(actionFlag){
            movie->stop();
            movie->setFileName("://"+playerName+"_jump.gif");
            movie->start();
            flip=true;
            actionFlag=false;
            jumpTimer->start(700);
        }
        body->SetLinearVelocity(b2Vec2(0.0f,speed));
    }
    else if(status==jumpRight){
        if(actionFlag){
            movie->stop();
            movie->setFileName("://"+playerName+"_jump_move.gif");
            movie->start();
            flip=false;
            actionFlag=false;
            jumpTimer->start(700);
        }
        body->SetLinearVelocity(b2Vec2(speed,speed));

    }else if(status==jumpLeft){
        qDebug()<<"actionFlag:"<<actionFlag<<endl;
        if(actionFlag){
            qDebug()<<"我在跳跃"<<endl;
            movie->stop();
            movie->setFileName("://"+playerName+"_jump_move.gif");
            movie->start();
            flip=true;
            actionFlag=false;
            jumpTimer->start(700);
        }
        body->SetLinearVelocity(b2Vec2(-speed,speed));
    }else if(status==standLeft){

        QString fileName="://"+playerName+"_stand.gif";
        if(movie->fileName()!=fileName){
            movie->stop();
            movie->setFileName(fileName);
            movie->start();
        }
        body->SetLinearVelocity(b2Vec2(0.0f,0.0f));
        flip=true;
        actionFlag=true;
    }else if(status==standRight){
        QString fileName="://"+playerName+"_stand.gif";
        if(movie->fileName()!=fileName){
            movie->stop();
            movie->setFileName(fileName);
            movie->start();
        }
        body->SetLinearVelocity(b2Vec2(0.0f,0.0f));
        flip=false;
        actionFlag=true;
    }else if(status==attackRight&&attackCD==0){
        qDebug()<<"attack";
        if(actionFlag){
            movie->stop();
            movie->setFileName("://"+playerName+"_attack.gif");
            movie->start();
            flip=false;
            attackGifTimer->start(400);


            if(areaActive&&agaistPlayer->getStatus()!=defenseLeft){
                if(agaistPlayer->getStatus()!=defenseLeft){
                    score+=10;
                    agaistPlayer->setStatus(attackedLeft);
                    if(skillPoint<3){
                         skillPoint+=1;
                    }
                }else{
                    agaistPlayer->setScore(agaistPlayer->getScore()+10);
                }
            }
            attackCD=4;
        }
    }else if(status==attackLeft&&attackCD==0){
        qDebug()<<"attackLeft";
        if(actionFlag){
        movie->stop();
        movie->setFileName("://"+playerName+"_attack.gif");
        movie->start();
        flip=true;
        attackGifTimer->start(400);

        if(areaActive){

            if(agaistPlayer->getStatus()!=defenseRight){
                score+=10;
                if(skillPoint<3){
                     skillPoint+=1;
                }
                agaistPlayer->setStatus(attackedRight);
            }else{
                agaistPlayer->setScore(agaistPlayer->getScore()+10);
            }

        }
        attackCD=4;
        }
     }else if(status==defenseRight&&defenseCD==0){
        movie->stop();
        flip=false;
        movie->setFileName("://"+playerName+"_defense.gif");
        movie->start();
        defenseCD=10;
    }
    else if(status==defenseLeft&&defenseCD==0){
            movie->stop();
            flip=true;
            movie->setFileName("://"+playerName+"_defense.gif");
            movie->start();
            defenseCD=10;
        }
    else if(status==combinationAttackRight){
        qDebug()<<"技能1"<<combinationAttackCD<<","<<skillPoint<<endl;
        if(skillPoint==3&&combinationAttackCD==0){
            qDebug()<<"开始施法"<<endl;
             QString fileName="://"+playerName+"_skill.gif";
            if(movie->fileName()!=fileName){
                movie->stop();
                movie->setFileName(fileName);
                movie->start();
                flip=false;
                skillGifTimer->start(2000);
                combinationAttackCD=150;
                skillPoint=0;
                if(areaActive){
                   score+=100;
                   agaistPlayer->setStatus(attackedLeft);
                }
            }
        }
    }
    else if(status==combinationAttackLeft){
        if(skillPoint==3&&combinationAttackCD==0){
            qDebug()<<"开始施法"<<endl;
             QString fileName="://"+playerName+"_skill.gif";
            if(movie->fileName()!=fileName){
                movie->stop();
                movie->setFileName(fileName);
                movie->start();
                flip=true;
                skillGifTimer->start(2000);
                combinationAttackCD=150;
                skillPoint=0;
                if(areaActive){
                   score+=100;
                   agaistPlayer->setStatus(attackedRight);
                }
            }
        }
    }else if(status==attackedLeft){
        QString fileName="://"+playerName+"_attacked.gif";
         qDebug()<<(movie->fileName()!=fileName)<<endl;
       if(movie->fileName()!=fileName){
           movie->stop();
           movie->setFileName(fileName);
           movie->start();
           flip=true;
           actionFlag=false;
           qDebug()<<"attacked gif l start"<<endl;
           attackedGifTimer->start(750);
       }
//       body->SetLinearVelocity(b2Vec2(-30.0f,0.0f));
    }else if(status==attackedRight){
        QString fileName="://"+playerName+"_attacked.gif";
        qDebug()<< movie->fileName()<<endl;
       if(movie->fileName()!=fileName){
           movie->stop();
           movie->setFileName(fileName);
           movie->start();
           flip=false;
           actionFlag=false;
           qDebug()<<"attacked gif r start"<<endl;
           attackedGifTimer->start(750);
       }
       graphics->setOffset(QPointF(-40,0));
//       body->SetLinearVelocity(b2Vec2(-30.0f,0.0f));
    }
    else if(this->getGraphics()->y()>-20){
        actionFlag=false;
    }
    QPixmap pixmap=movie->currentPixmap();
    QImage img=movie->currentImage();
    QImage mir=img.mirrored(true,false);
    if(flip){
         graphics->setPixmap(QPixmap::fromImage(mir));
    }else{
        graphics->setPixmap(QPixmap::fromImage(img));
    }

    x=pos.x;
    y=-pos.y;
    graphics->setPos(x*10,y*10);

}



void Player::init(){
    score=0;
    qDebug()<<" 初始化人物成功";
    attackCD=0;
    defenseCD=0;
    combinationAttackCD=0;
    skillPoint=0;
}

void Player::setStatus(PlayerStatus status){
    this->status=status;
}

PlayerStatus Player::getStatus(){
    return status;
}

QGraphicsPixmapItem* Player::getGraphics(){
    if(graphics==nullptr){
        graphics->setPixmap(movie->currentPixmap());
    }
    return graphics;
}

void Player::setGraphics(QGraphicsPixmapItem *graphics){
    this->graphics=graphics;
}

void Player::setBody(b2Body *body){
    this->body=body;
}

int Player::getScore(){
    return score;
}
void Player::setScore(int score){
    this->score=score;
}

void Player::clearPoint(){
    skillPoint=0;
}



void Player::setpointLabel1(QLabel *pointLabel1){
    this->pointLabel1=pointLabel1;
}
void Player::setpointLabel2(QLabel *pointLabel2){
    this->pointLabel2=pointLabel2;
}
void Player::setpointLabel3(QLabel *pointLabel3){
    this->pointLabel3=pointLabel3;
}

QLabel *Player::getpointLabel1(){
    return pointLabel1;
}
QLabel *Player::getpointLabel2(){
    return pointLabel2;
}
QLabel *Player::getpointLabel3(){
    return pointLabel3;
}


b2Body* Player::getBody(){
    return body;
}

b2Vec2 Player::getDirection(){
    if(status==standLeft||status==moveLeft||status==jumpLeft||status==jumpUpLeft||status==jumpingLeft||status==attackLeft||status==defenseLeft||status==combinationAttackLeft){
        return b2Vec2(-1,0);
    }else{
        return b2Vec2(1,0);
    }
}

void Player::setRayCast(RayCastController *raycast){
    this->raycast=raycast;
}


int Player::getSkillPoint(){
    return skillPoint;
}

///获取受击次数
int  Player::getAttackedNum(){
    return attackedNum;
}
///设置受击次数变化
void Player::setAttackedNum(){
    if(attackedNum<3){
        if(this->getStatus()==attackedLeft||this->getStatus()==attackedRight)
            attackedNum++;
    }
    else if(attackedNum==4)
        attackedNum=0;
}
///设置行动标志
void Player::setActionFlag(bool actionFlag){
    this->actionFlag=actionFlag;
}
///获得行动标志
bool Player::getActionFlag(){
    return actionFlag;
}
///设置QMovie
void Player::setMovie(QMovie *movie){
    this->movie=movie;
}
///获得QMovie
QMovie* Player::getMovie(){
    return movie;
}
///设置玩家名
void Player::setPlayerName(QString playerName){
    this->playerName=playerName;
}
///获得玩家名
QString Player::getPlayerName(){
    return this->playerName;
}

void Player::setAgainstPlayer(Player* p){
    this->agaistPlayer=p;
}

int Player::getCombinationAttackCD(){
    return combinationAttackCD;
}

void Player::setAreaActive(bool is){
    this->areaActive=is;
}















